<template>
  <div ref="dom"/>
</template>

<script>
import echarts from 'echarts'
import { on, off } from '@/libs/tools'

export default {
  name: 'ChartBar',
  data() {
    return {
      dom: null
    }
  },
  props: {
    value: Object,
    width: Number,
    height: Number
  },
  methods: {
    resize() {
      this.dom.resize()
    },
    showBar() {
      let option = {
        title: {
          text: '访问量趋势'
        },
        color: ['#44cdff'],
        tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross',
              label: {
                backgroundColor: '#6a7985'
              }
            }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          data: [120, 200, 150, 80, 70, 110, 130],
          type: 'bar',
          itemStyle: {
            normal: {
              barBorderRadius:[5, 5, 0, 0],
            }
          }
        }]
      }

      this.$nextTick(() => {
        this.dom = echarts.init(this.$refs.dom)
        this.dom.setOption(option)
        on(window, 'resize', this.resize())
      })
    }
  },
  watch: {
    value() {
      this.showBar()
    }
  },
  mounted() {
    this.showBar()
  },
  beforeDestroy() {
    off(window, 'resize', this.resize())
  }
}
</script>